package srzy.module

import java.io.FileWriter
import java.io.StringWriter
import srzy.driver.Start
import java.io.File

object FileRec extends AbsCaseDeal with srzy.common.Logging {

  //  val fileName1 =sys.env.getOrElse("RBAutoTestTestInfo", "TestInfo.txt")
  //  val fileName2 = sys.env.getOrElse("RBAutoTestErrorCase", "ErrorCase.txt")

  var fileName1 = "TestInfo.txt"
  var fileName2 = "ErrorCase.txt"

  val f = new File("etc/filename")
  if (f.exists()) {
    val txt = srzy.common.Common.readFromFile(f).mkString("")
    val arr = txt.split(" ").toList
    if (arr.length == 2) {
      fileName1 = arr.apply(0)
      fileName2 = arr.apply(1)
    } else {
      logger.error("etc/filename [" + txt + "] error")
    }
  }

  val fileName3 = sys.env.getOrElse("RBAutoTestDealedCase", "DealedCase.txt")

  logger.debug("fileName1:" + fileName1)
  logger.debug("fileName2:" + fileName2)
  logger.debug("fileName3:" + fileName3)

  def getText1(): String = {
    val sw = new StringWriter
    val caseInfo = getCaseInfo
    sw.append(caseInfo.caseName)
    sw.append("\n")
    sw.append("casepath=" + caseInfo.casePath)
    sw.append("\n")
    sw.append("FinalCompareResult=" + caseInfo.caseResult)
    sw.append(" ")
    sw.append("MsgCompareResult=" + caseInfo.msgCompResult)
    sw.append(" ")
    sw.append("DBCompareResult=" + caseInfo.dbCompResult)
    sw.append(" ")
    sw.append("ShellCompareResult=" + caseInfo.shellCompResult)
    sw.append(" ")
    sw.append("DBInitResult=" + caseInfo.dbInitResult)
    sw.append(" ")
    sw.append("ShellInitResult=" + caseInfo.shellInitResult)
    sw.append("\n")
    sw.append("[InitShell]" + "\n" + caseInfo.initShell)
    sw.append("\n")
    // for (String s : caseInfo.getDbInitSQL()) {
    // sw.append(s + "\n")
    // }
    // sw.append("\n")
    sw.append("[SendMsg]" + "\n")
    sw.append(caseInfo.sendMsg.mkString("\n"))
    sw.append("\n")
    sw.append("[ReceMsg]" + "\n")
    sw.append(caseInfo.receMsg.mkString("\n"))
    sw.append("\n")
    sw.append("[ReceStdMsg]" + "\n")
    sw.append(caseInfo.receStdMsg.mkString("\n"))
    sw.append("\n")
    sw.append("[MsgCompInfo]" + "\n")
    sw.append(caseInfo.msgCompInfos.mkString("\n"))
    sw.append("\n")
    sw.append("[DbCompInfo]" + "\n")
    sw.append(caseInfo.dbCompInfos.mkString("\n"))
    sw.append("\n")
    sw.append("[ShellCompInfo]" + "\n")
    sw.append(caseInfo.shellCompInfos.mkString("\n"))
    sw.append("\n")
    sw.append("\n")
    sw.toString()
  }

  def getText2(): String = {
    if (getCaseInfo.caseResult == false) {
      getCaseInfo.casePath + "\n"
    } else {
      ""
    }
  }

  def getText3(): String = {
    (caseId + 1) + " " + getCaseInfo.casePath + "\n"
  }

  val fileWriters: List[(String, java.io.FileWriter, () => String)] = List(
    (fileName1, new FileWriter(fileName1, false), getText1),
    (fileName2, new FileWriter(fileName2, false), getText2),
    (fileName3, new FileWriter(fileName3, false), getText3))

  def deal = {
    try {
      fileWriters.foreach(e => {
        logger.debug("begin write " + e._1)
        logger.debug("caseId:" + caseId)
        e._2.write(e._3())
        e._2.flush()
        logger.debug("end write " + e._1)
      })

    } catch {
      case ex: Exception => logger.error(ex.getMessage(), ex)
    }
  }

}